<?php
/**
 * 联系我们页面
 * 州弟医院信息系统 - 州弟学安全
 */

// 设置页面标题
$pageTitle = '联系我们';

// 包含头文件
include 'includes/header.php';

// 包含数据库连接
require_once 'includes/config.php';

// 包含函数文件和数据库连接
require_once 'includes/functions.php';

// 处理联系表单提交
$message = '';
$message_type = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit'])) {
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $phone = trim($_POST['phone']);
    $subject = trim($_POST['subject']);
    $message_content = trim($_POST['message']);
    
    // 简单验证
    if (empty($name) || empty($email) || empty($phone) || empty($subject) || empty($message_content)) {
        $message = '请填写所有必填字段';
        $message_type = 'danger';
    } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $message = '请输入有效的电子邮件地址';
        $message_type = 'danger';
    } else {
        // 存储联系信息到数据库
        try {
            // 使用mysqli预处理语句替换PDO语法
            $name = $conn->real_escape_string($name);
            $email = $conn->real_escape_string($email);
            $phone = $conn->real_escape_string($phone);
            $subject = $conn->real_escape_string($subject);
            $message_content = $conn->real_escape_string($message_content);
            
            $sql = "INSERT INTO contact_messages (name, email, phone, subject, message, created_at) 
                    VALUES (?, ?, ?, ?, ?, NOW())";
            
            $stmt = $conn->prepare($sql);
            
            if ($stmt) {
                $stmt->bind_param("sssss", $name, $email, $phone, $subject, $message_content);
                
                if ($stmt->execute()) {
                    $message = '感谢您的留言，我们会尽快回复您！';
                    $message_type = 'success';
                    
                    // 记录系统日志
                    $log_desc = "收到来自 {$name} ({$email}) 的留言";
                    $ip = $_SERVER['REMOTE_ADDR'];
                    logAction(null, $log_desc, $ip);
                    
                    // 清空表单
                    $name = $email = $phone = $subject = $message_content = '';
                } else {
                    $message = '提交留言时出现错误，请稍后再试';
                    $message_type = 'danger';
                }
                $stmt->close();
            } else {
                $message = '系统错误，请稍后再试';
                $message_type = 'danger';
            }
        } catch (Exception $e) {
            $message = '系统错误，请稍后再试';
            $message_type = 'danger';
        }
    }
}

// 处理表单提交
$success = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['contact_submit'])) {
    $name = $_POST['name'] ?? '';
    $email = $_POST['email'] ?? '';
    $phone = $_POST['phone'] ?? '';
    $subject = $_POST['subject'] ?? '';
    $message = $_POST['message'] ?? '';
    
    // 简单验证
    if (empty($name) || empty($email) || empty($message)) {
        $error = '姓名、邮箱和留言内容不能为空';
    } elseif (!isValidEmail($email)) {
        $error = '请输入有效的邮箱地址';
    } elseif (!empty($phone) && !isValidPhone($phone)) {
        $error = '请输入有效的手机号码';
    } else {
        // 检查数据库中是否存在联系信息表
        $tableExists = false;
        $result = $conn->query("SHOW TABLES LIKE 'contact_messages'");
        if ($result && $result->num_rows > 0) {
            $tableExists = true;
        }

        // 如果表不存在，创建它
        if (!$tableExists) {
            $createTable = "CREATE TABLE IF NOT EXISTS contact_messages (
                id INT AUTO_INCREMENT PRIMARY KEY,
                name VARCHAR(100) NOT NULL,
                email VARCHAR(100) NOT NULL,
                phone VARCHAR(20),
                subject VARCHAR(200),
                message TEXT NOT NULL,
                is_read BOOLEAN DEFAULT FALSE,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
            
            $conn->query($createTable);
        }

        // 保存联系信息
        $name = $conn->real_escape_string($name);
        $email = $conn->real_escape_string($email);
        $phone = $conn->real_escape_string($phone);
        $subject = $conn->real_escape_string($subject);
        $message = $conn->real_escape_string($message);
        
        $sql = "INSERT INTO contact_messages (name, email, phone, subject, message) 
                VALUES ('$name', '$email', '$phone', '$subject', '$message')";
        
        if ($conn->query($sql)) {
            $success = '您的留言已成功提交，我们将尽快与您联系！';
            
            // 记录系统日志
            $user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null;
            logAction($user_id, '提交联系表单', '用户提交了联系表单：' . $subject);
            
            // 清空表单
            $name = $email = $phone = $subject = $message = '';
        } else {
            $error = '提交失败，请稍后再试：' . $conn->error;
        }
    }
}
?>

<div class="container-fluid py-5 bg-light">
    <div class="container">
        <div class="row mb-5">
            <div class="col-md-10 col-lg-8 mx-auto text-center">
                <h1 class="display-4 text-primary mb-3">联系我们</h1>
                <p class="lead text-muted">如有任何问题或建议，欢迎通过以下方式与我们取得联系，我们将竭诚为您服务</p>
            </div>
        </div>
        
        <?php if (!empty($message)): ?>
        <div class="row">
            <div class="col-md-10 col-lg-8 mx-auto">
                <div class="alert alert-<?php echo $message_type; ?> alert-dismissible fade show" role="alert">
                    <?php echo $message; ?>
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
            </div>
        </div>
        <?php endif; ?>
        
        <div class="row">
            <div class="col-lg-5 mb-5 mb-lg-0">
                <div class="card shadow-sm h-100">
                    <div class="card-body">
                        <h3 class="h4 mb-4 text-primary">联系方式</h3>
                        
                        <div class="d-flex mb-4">
                            <div class="mr-3">
                                <i class="fas fa-map-marker-alt text-primary fa-2x"></i>
                            </div>
                            <div>
                                <h5 class="h6 mb-1">医院地址</h5>
                                <p class="text-muted mb-0">北京市海淀区西二旗科技园区</p>
                            </div>
                        </div>
                        
                        <div class="d-flex mb-4">
                            <div class="mr-3">
                                <i class="fas fa-phone-alt text-primary fa-2x"></i>
                            </div>
                            <div>
                                <h5 class="h6 mb-1">联系电话</h5>
                                <p class="text-muted mb-0">门诊预约: 010-12345678</p>
                                <p class="text-muted mb-0">急诊热线: 010-87654321</p>
                            </div>
                        </div>
                        
                        <div class="d-flex mb-4">
                            <div class="mr-3">
                                <i class="fas fa-envelope text-primary fa-2x"></i>
                            </div>
                            <div>
                                <h5 class="h6 mb-1">电子邮箱</h5>
                                <p class="text-muted mb-0">contact@zhoudihospital.com</p>
                                <p class="text-muted mb-0">support@zhoudihospital.com</p>
                            </div>
                        </div>
                        
                        <div class="d-flex mb-4">
                            <div class="mr-3">
                                <i class="fas fa-clock text-primary fa-2x"></i>
                            </div>
                            <div>
                                <h5 class="h6 mb-1">工作时间</h5>
                                <p class="text-muted mb-0">门诊: 周一至周五 8:00-17:30</p>
                                <p class="text-muted mb-0">急诊: 24小时</p>
                            </div>
                        </div>
                        
                        <div class="mt-4">
                            <h5 class="h6 mb-3">关注我们的社交媒体</h5>
                            <div class="d-flex">
                                <a href="#" class="social-link" title="微信">
                                    <i class="fab fa-weixin fa-2x text-primary mr-3"></i>
                                </a>
                                <a href="#" class="social-link" title="微博">
                                    <i class="fab fa-weibo fa-2x text-primary mr-3"></i>
                                </a>
                                <a href="#" class="social-link" title="抖音">
                                    <i class="fab fa-tiktok fa-2x text-primary"></i>
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-lg-7">
                <div class="card shadow-sm">
                    <div class="card-body">
                        <h3 class="h4 mb-4 text-primary">留言反馈</h3>
                        
                        <?php if ($success): ?>
                        <div class="alert alert-success"><?php echo $success; ?></div>
                        <?php endif; ?>
                        
                        <?php if ($error): ?>
                        <div class="alert alert-danger"><?php echo $error; ?></div>
                        <?php endif; ?>
                        
                        <form method="POST" action="contact.php">
                            <div class="form-row">
                                <div class="form-group col-md-6">
                                    <label for="name">姓名 <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" id="name" name="name" value="<?php echo isset($name) ? htmlspecialchars($name) : ''; ?>" required>
                                </div>
                                <div class="form-group col-md-6">
                                    <label for="email">电子邮箱 <span class="text-danger">*</span></label>
                                    <input type="email" class="form-control" id="email" name="email" value="<?php echo isset($email) ? htmlspecialchars($email) : ''; ?>" required>
                                </div>
                            </div>
                            
                            <div class="form-row">
                                <div class="form-group col-md-6">
                                    <label for="phone">电话 <span class="text-danger">*</span></label>
                                    <input type="tel" class="form-control" id="phone" name="phone" value="<?php echo isset($phone) ? htmlspecialchars($phone) : ''; ?>" required>
                                </div>
                                <div class="form-group col-md-6">
                                    <label for="subject">主题 <span class="text-danger">*</span></label>
                                    <select class="form-control" id="subject" name="subject" required>
                                        <option value="" disabled <?php echo !isset($subject) ? 'selected' : ''; ?>>请选择主题</option>
                                        <option value="预约咨询" <?php echo (isset($subject) && $subject == '预约咨询') ? 'selected' : ''; ?>>预约咨询</option>
                                        <option value="就医建议" <?php echo (isset($subject) && $subject == '就医建议') ? 'selected' : ''; ?>>就医建议</option>
                                        <option value="意见反馈" <?php echo (isset($subject) && $subject == '意见反馈') ? 'selected' : ''; ?>>意见反馈</option>
                                        <option value="其他问题" <?php echo (isset($subject) && $subject == '其他问题') ? 'selected' : ''; ?>>其他问题</option>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <label for="message">留言内容 <span class="text-danger">*</span></label>
                                <textarea class="form-control" id="message" name="message" rows="5" required><?php echo isset($message_content) ? htmlspecialchars($message_content) : ''; ?></textarea>
                            </div>
                            
                            <div class="text-right">
                                <button type="submit" name="submit" class="btn btn-primary btn-lg">
                                    <i class="fas fa-paper-plane mr-2"></i>提交留言
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
                
                <div class="card shadow-sm mt-4">
                    <div class="card-body">
                        <h3 class="h4 mb-3 text-primary">医院位置</h3>
                        <div class="embed-responsive embed-responsive-16by9">
                            <iframe class="embed-responsive-item" src="https://maps.google.com/maps?q=北京市海淀区西二旗&t=&z=13&ie=UTF8&iwloc=&output=embed" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
// 包含尾文件
include 'includes/footer.php';
?> 